% using watersged method to separate the bulk detection to indivadual ones
function af_fg_bw = my_WatershedSeg(fg_bw, type)
D = -bwdist(~fg_bw);
if strcmp(type, 'Microscope'),  
    mask = imextendedmin(D, 8); % for the microscope images
elseif strcmp(type, 'Scanscope'),
    mask = imextendedmin(D, 2); % for the scanscope images
end
% figure;
% imshowpair(fg_bw, mask, 'blend');
D2 = imimposemin(D, mask);
Ld = watershed(D2);
bw = fg_bw;
bw(Ld == 0) = 0;
% figure;
% imagesc(bw); colormap(gray);
af_fg_bw = bwareaopen(bw, 10);
% af_fg_bw = bw;
return;

